﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace Corsat.Library.DataManager
{
    public static partial class Main
    {
        public static DataTable GetCourseSerials(int CourseId, int Year)
        {
            #region Select command Text

            command.CommandText = StoredProcedure.sp_GetCourseAccounts.ToString();
            command.CommandType = CommandType.StoredProcedure;

            #endregion

            #region Parameters

            command.Parameters.Clear();
            command.Parameters.Add("@CourseId", SqlDbType.Int).Value = CourseId;
            command.Parameters.Add("@Year", SqlDbType.Int).Value = Year;

            #endregion

            #region Get Data

            try
            {
                DataTable dtSerials = new DataTable();

                adapter.Fill(dtSerials);

                if (dtSerials != null && dtSerials.Rows.Count > 0)
                    return dtSerials;
                else
                    return null;
            }
            catch (Exception exc)
            {
                return null;
            }

            #endregion
        }

        

        public static void AdjustSerials(int CourseId, int Year, List<KeyValuePair<object, object>> Serials)
        {
            command.CommandText = "";
            command.CommandType = CommandType.Text;
            command.Parameters.Clear();

            for (int i = 0; i < Serials.Count; i++)
            {
                command.CommandText += @"

                UPDATE AccountCourses SET AccountSerialInCourse = ";

                if (Serials[i].Value == null || Serials[i].Value == DBNull.Value || Serials[i].Value == "")
                    command.CommandText += " NULL ";
                else
                    command.CommandText += Convert.ToInt32(Serials[i].Value).ToString();

                command.CommandText += " WHERE CourseId = " + CourseId.ToString() + " AND Year = " + Year.ToString() + " AND AccountId = " + Serials[i].Key.ToString();
            }

            OpenConnection();

            command.ExecuteNonQuery();

            CloseConnection();
        }
    }
}

